<template>
	<uni-popup ref="popup" type="bottom">
		<view class="content">
			<view class="hd">
				<text>服务关键词</text>
				<icon class="iconfont icon" @click="close()">&#xe733;</icon>
			</view>
			<view class="bd">
				<view class="list">
					<view class="item" v-for="(item, index) in keyWordData.labelData" :key="index">
						<text>{{ item }}</text>
						<icon class="iconfont icon" @click="deleteKeyWord(index)">&#xe733;</icon>
					</view>
				</view>
				<view v-if="!keyWordData.isShow">
					<button class="custom-btn" @click="isShowInput">
						<i class="iconfont icon">&#xe71e;</i>
						自定义
					</button>
				</view>
				<view class="input-keyword" v-if="keyWordData.isShow">
					<input class="input" type="text" placeholder="请输入3-8个字" v-model="keyWordData.keyWordValue" maxlength="8" placeholder-class="placeholderStyle"/>
					<button class="add-keyword" @click="addKeyWord">添加</button>
				</view>
			</view>
			<view class="ft"><button class="btn btn-sub" hover-class="btnClickStyle" @click="pushKeyWordData">确定</button></view>
		</view>
	</uni-popup>
</template>

<script setup>
import { ref, reactive } from 'vue';

const popup = ref(null);
const emits = defineEmits(['keyWordLayerCallback']);
const keyWordData = reactive({
	labelData: [],
	isShow: false,
	keyWordValue: ''
});

defineExpose({
	open,
	close
});
//自定义添加
function isShowInput() {
	if (keyWordData.labelData.length < 3) {
		keyWordData.isShow = !keyWordData.isShow;
	} else {
		toastLayer('最多添加三个服务关键词');
	}
	keyWordData.keyWordValue = '';
}
//删除
function deleteKeyWord(index) {
	keyWordData.labelData.splice(index, 1);
}
//添加关键词
function addKeyWord() {
	let newStr =  keyWordData.keyWordValue.replace(/\s/g,"")
	if (newStr.length < 3) {
		toastLayer('请输入3-8个字');
	} else if (keyWordData.labelData.includes(newStr)) {
		toastLayer('已有相同服务关键词，请重新输入');
	} else {
		keyWordData.labelData.push(newStr);
		newStr = '';
		keyWordData.isShow = !keyWordData.isShow;
	}
}
//提交
async function pushKeyWordData() {
	emits('keyWordLayerCallback', keyWordData.labelData);
}

function open(data) {
	popup.value.open();
	keyWordData.labelData = data;
}
function close() {
	popup.value.close();
}
function toastLayer(text) {
	uni.showToast({
		icon: 'none',
		title: text
	});
}
</script>

<style scoped lang="scss">
@import '@/common/css/base.scss';
.content {
	width: 100%;
	padding-bottom: 32rpx;
	border-radius: 16rpx;
	border-bottom-left-radius: 0;
	border-bottom-right-radius: 0;
	background-color: #fff;
	.hd {
		@include flex(center, space-between);
		padding: 38rpx 26rpx;
		border-bottom: 1rpx solid #ddd;
		font-size: 36rpx;
		font-weight: bold;
		.icon {
			font-size: 36rpx;
			color: #666;
		}
	}
	.bd {
		padding: 38rpx 28rpx 0 28rpx;
		margin-bottom: 36rpx;
		.icon {
			display: inline-block;
			font-size: 32rpx;
		}
		.item {
			display: inline-block;
			margin: 0 22rpx 22rpx 0;
			padding: 0 22rpx;
			height: 64rpx;
			line-height: 64rpx;
			font-size: 28rpx;
			color: #666;
			background: #f5f5f5;
			border-radius: 40rpx;
			.icon {
				margin-left: 22rpx;
			}
		}
		.custom-btn {
			display: inline-block;
			width: 202rpx;
			height: 64rpx;
			line-height: 64rpx;
			color: #e1835b;
			font-size: 28rpx;
			background-color: #fff;
			border: 1rpx dashed #e1835b;
			border-radius: 40rpx;
		}
		.input-keyword {
			@include flex(center, space-between);
			width: 100%;
			border-bottom: 1px solid #ddd;
			.input,
			.add-keyword {
				height: 84rpx;
				line-height: 84rpx;
			}
			.input {
			}
			.add-keyword {
				display: inline-block;
				margin: 0;
				background-color: #fff;
				color: #e1835b;
			}
		}
	}
	.btn-sub {
		width: 695rpx;
		height: 80rpx;
		line-height: 80rpx;
		color: #fff;
		background: #e1835b;
		border-radius: 40rpx;
	}
}
</style>
